import logging

from fastapi import APIRouter, Query

from app.controllers.category import category_controller
from app.schemas.categories import *
from app.core.dependency import DependPermission
from app.schemas.base import Fail, Success, SuccessExtra

logger = logging.getLogger(__name__)

router = APIRouter()


@router.get("/list",
            summary="获取分类列表",
            description="获取分类列表",
            dependencies=[DependPermission])
async def list_categroy(page: int = Query(1, description="页码"),
                        page_size: int = Query(10, description="每页数量")):
    """
    获取分类列表
    """
    parent_categories = await category_controller.model.filter(
        parent_id=0).order_by("order")
    res_category = []

    for category in parent_categories:
        child_category = await category_controller.model.filter(
            parent_id=category.id).order_by("order")
        category_dict = await category.to_dict()
        category_dict["children"] = [
            await obj.to_dict() for obj in child_category
        ]
        res_category.append(category_dict)
    return SuccessExtra(data=res_category,
                        total=len(res_category),
                        page=page,
                        page_size=page_size)


@router.get("/get/id", summary="获取分类信息")
async def get_category(category_id: int = Query(..., description="分类id")):
    result = await category_controller.model.filter(id=category_id)
    if len(result) > 0:
        result = result[0].to_dict()
        return Success(data=result)
    else:
        return Fail(message=f"找不到ID为{category_id}的分类")


@router.get("/get/alias", summary="获取分类别名")
async def get_category_alias(alias: str = Query(..., description="分类别名")):
    result = await category_controller.model.filter(alias=alias)
    if len(result) > 0:
        result = await result[0].to_dict()
        return Success(data=result)
    else:
        return Fail(message=f"找不到别名为{alias}的分类")


@router.post("/create",
             summary="创建分类",
             description="创建分类",
             dependencies=[DependPermission])
async def create_category(category_in: CategoryCreate):
    """
    创建分类
    """
    await category_controller.create(obj_in=category_in)
    return Success(message="创建分类成功")


@router.post("/update",
             summary="更新分类",
             description="更新分类",
             dependencies=[DependPermission])
async def update_category(category_in: CategoryUpdate):
    await category_controller.update(obj_in=category_in)
    return Success(message="更新分类成功")


@router.post("/delete",
             summary="删除分类",
             description="删除分类",
             dependencies=[DependPermission])
async def delete_category(category_id: int = Query(..., description="分类id")):
    child_category_count = await category_controller.model.filter(
        parent_id=category_id).count()
    if len(child_category) > 0:
        return Fail(message="该分类下有子分类，无法删除")
    await category_controller.delete(id=category_id)
    return Success(message="删除分类成功")
